import config from "config";
import httpFetch from "share/httpFetch";
import errorMessage from "share/errorMessage";

export default {
  //得到报销单审核列表
  getAuditExpenseList(page, pageSize, sort = "", params) {
    return httpFetch
      .post(
        `${config.baseUrl}/api/v2/expense/reports/finance/admin/search?page=${page}&size=${pageSize}&sort=${sort}`,
        params
      )
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //得到借款单审核列表
  getAuditBorrowList(page, pageSize, sort = "", params) {
    return httpFetch
      .post(
        `${config.baseUrl}/api/loanBill/finance/query/by/page?page=${page}&size=${pageSize}&sort=${sort}`,
        params
      )
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //得到报销单审核列表
  getAuditCorporateList(page, pageSize, sort = "", params) {
    return httpFetch
      .post(
        `${config.baseUrl}/api/v2/expense/reports/finance/admin/search?page=${page}&size=${pageSize}&sort=${sort}`,
        params
      )
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //免打印查询
  searchPrintFree(tenantId, params) {
    return httpFetch.post(
      `${config.baseUrl}/config/api/config/hit/EXPENSE_REPORT_PRINT_FREE?tenantId=${tenantId}`,
      params
    );
  },

  //审核通过
  auditPass(params) {
    return httpFetch.post(`${config.baseUrl}/api/audit/pass`, params, {}, { timeout: 180000 });
  },

  //审核拒绝
  auditReject(params) {
    return httpFetch.post(`${config.baseUrl}/api/audit/reject`, params);
  },

  //审核通知
  noticeApplication(params, entityType) {
    let url =
      entityType === 5001
        ? "/api/expense/report/bag/send/notice"
        : "/api/approval/send/notice";
    return httpFetch.post(url, params);
  },

  //审核图片
  checkAttachment(params) {
    return httpFetch.post(`${config.baseUrl}/api/audit/image/checked`, params);
  },

  //发票袋查询
  getExpCodeOption(expenseReportBagCode, isAudit) {
    return httpFetch.get(
      `${config.baseUrl}/api/expense/reports/bag/fuzzy/query?expenseReportBagCode=${expenseReportBagCode}&isAudit=${isAudit}`
    );
  },

  //报销单内发票
  getExpenseReportInvoices(expenseReportOID) {
    return httpFetch
      .get(`${config.baseUrl}/api/expense/reports/receipt/finance/query`, {
        expenseReportOID,
      })
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //得到发票详情
  getInvoiceDetail(receiptId) {
    return httpFetch
      .get(`${config.baseUrl}/api/receipt/query/${receiptId}`)
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //OCR扫描发票图片
  scanBase64(params, entityId) {
    return httpFetch
      .post(
        `${config.baseUrl}/api/expense/reports/bag/receipt/scan`,
        {
          ...params,
          entityId,
          entityType: "expense_report",
        },
        {},
        {
          timeout: 60000,
        }
      )
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //删除财务发票
  deleteFinanceInvoice(receiptId) {
    return httpFetch
      .delete(`${config.baseUrl}/receipt/api/receipts/${receiptId}`)
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //得到批注
  getAnnotateByEntityOID(entityOID, type) {
    return httpFetch
      .get(`${config.baseUrl}/api/report/note/${entityOID}`, { type })
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //新增批注
  addAnnotate(params) {
    return httpFetch
      .post(`${config.baseUrl}/api/report/note/save`, params)
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //删除批注
  deleteAnnotate(noteId) {
    return httpFetch
      .delete(`${config.baseUrl}/api/report/note/${noteId}`)
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //核对发票
  checkInvoice(params) {
    return httpFetch
      .post(`${config.baseUrl}/api/audit/receipt/checked`, params)
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //单笔驳回
  rejectExpense(params) {
    return httpFetch
      .post(`/api/audit/reject/expensereport/invoice`, params)
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //报销单核对发票
  expenseCheckInvoice(expenseReportOID) {
    return httpFetch
      .put(
        `/api/expense/reports/receipt/check?expenseReportOID=${expenseReportOID}`
      )
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },

  //获取人工核对信息 type: NOT_MATCHED(未匹配)，TO_BE_CHECKED(待核对)
  getManualCheckInfo(entityOID, type, entityType) {
    let url =
      entityType === 5001
        ? "/api/expense/report/bag/manual/check"
        : "/api/expense/report/manual/check";
    let params =
      entityType === 5001
        ? { expenseReportBagCode: entityOID, type }
        : { expenseReportOID: entityOID, type };
    return httpFetch.get(url, params);
  },

  //人工发票匹配／核对
  manualCheck(params, entityType) {
    let url =
      entityType === 5001
        ? "/api/expense/report/bag/manual/check"
        : "/api/expense/report/manual/check";
    return httpFetch.post(url, params);
  },

  //获取通知的发票信息
  getNoticeInfo(entityOID, entityType) {
    let url =
      entityType === 5001
        ? "/api/expense/report/bag/notice/message"
        : "/api/expense/report/notice/message";
    let params =
      entityType === 5001
        ? { expenseReportBagCode: entityOID }
        : { expenseReportOID: entityOID };
    return httpFetch.get(url, params);
  },

  getAuditFlag(params) {
    return httpFetch.get("/api/audit/flag", params);
  },

  //扫码枪审核 start
  // 单据
  scan(params) {
    return httpFetch.post(`${config.baseUrl}/api/audit/scancode`, params);
  },
  // 发票袋
  scanBag(params) {
    return httpFetch.post(
      `${config.baseUrl}/api/expense/reports/bag/web/scancode`,
      params
    );
  },
  // 发票袋驳回
  rejectBag(params) {
    return httpFetch.post(
      `${config.baseUrl}/api/expense/reports/bag/scan/reject`,
      params
    );
  },

  //审核驳回：发票袋／单据
  rejectAudit(params) {
    return httpFetch.post(`/api/audit/scan/reject`, params);
  },

  // 移除发票袋关联关系
  deleteBagRelation(params) {
    return httpFetch
      .post(`/api/expense/report/bag/delete/relation`, params)
      .catch((e) => {
        errorMessage(e.response);
        throw e;
      });
  },
};
